'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }

  async getlist() {
    const { ctx, app } = this;
    const data = await app.mysql.select('tabelist')
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  async getdel() {
    const { ctx, app } = this;
    const data = await app.mysql.delete('tabelist', { id: ctx.request.body.id })
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  async search() {
    const { ctx, app } = this;
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from tabelist where name like "%${value}%"`)
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }

  async append() {
    const { ctx, app } = this;
    const { id, title, name, sold, skinge, family, bone, skin, heart, mass, doctor, stoplist, wifelist, skprice, sktype, sum, boy } = ctx.request.body;
    const data = await app.mysql.insert('tabelist', { id, title, name, sold, skinge, family, bone, skin, heart, mass, doctor, stoplist, wifelist, skprice, sktype, sum, boy });
    ctx.body = {
      code: 200,
      msg: "成功",
      data
    }
  }


  async amend() {
    const { app, ctx } = this
    const { name, title, id, sold, skinge, family, bone, skin, heart, mass, doctor, stoplist, wifelist, skprice, sktype, sum, boy } = ctx.request.body
    const data = await app.mysql.update('tabelist', { name, title, id, sold, skinge, family, bone, skin, heart, mass, doctor, stoplist, wifelist, skprice, sktype, sum, boy })

    ctx.body = {
      code: 1,
      data
    }
  }
}

module.exports = HomeController;
